package cn.tedu.erp.project.controller;

import cn.tedu.erp.project.pojo.dto.RoleAddNewDTO;
import cn.tedu.erp.project.pojo.dto.RoleUpdateDTO;
import cn.tedu.erp.project.pojo.entity.RolePermission;
import cn.tedu.erp.project.pojo.vo.PermissionListVO;
import cn.tedu.erp.project.pojo.vo.RoleListVO;
import cn.tedu.erp.project.pojo.vo.RoleStandardVO;
import cn.tedu.erp.project.service.IRoleService;
import cn.tedu.erp.project.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/roles")
@Api(tags = "03.角色管理模块")
public class RoleController {

    public RoleController() {
        log.debug("创建控制器对象:RoleController");
    }

    @Autowired
    IRoleService service;

    //http://localhost:9090/roles/add-new
    @PostMapping("/add-new")
    @ApiOperation("添加角色")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAuthority('/erp_pwdb/role/add-new')")
    public JsonResult<Void> addNew(RoleAddNewDTO roleAddNewDTO) {
        log.debug("开始处理[添加角色]的请求,请求参数:{}", roleAddNewDTO);
        service.addNew(roleAddNewDTO);
        return JsonResult.ok();
    }

    //http://localhost:9090/roles/0-9/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("删除角色")
    @PreAuthorize("hasAuthority('/erp_pwdb/role/delete')")//检查用户是否有权限
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "角色ID", required = true, dataType = "long")
    )
    public JsonResult<Void> delete(@PathVariable @Range(min = 2, message = "请提交有效的ID值!") Long id) {
        log.debug("开始处理[删除角色]的请求,请求参数:{}", id);
        service.deleteById(id);
        return JsonResult.ok();
    }

    //http://localhost:9090/roles/0-9/update
    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改角色")
    @PreAuthorize("hasAuthority('/erp_pwdb/role/update')")//检查用户是否有权限
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "角色ID", required = true, dataType = "long")
    )
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 2, message = "请提交有效的ID值!") Long id,
                                           @Valid RoleUpdateDTO roleUpdateDTO) {
        log.debug("开始处理修改角色的请求,参数为id={},修改的数据={}", id, roleUpdateDTO);
        service.updateById(id, roleUpdateDTO);
        return JsonResult.ok();
    }


    //http://localhost:9090/roles
    @GetMapping("")
    @ApiOperation("查询角色列表")
    @PreAuthorize("hasAuthority('/erp_pwdb/role/read')")//检查用户是否有权限
    @ApiOperationSupport(order = 410)
    public JsonResult<List<RoleListVO>> list() {
        log.debug("开始处理[查询角色]的请求,无参数");
        List<RoleListVO> list = service.list();
        return JsonResult.ok(list);
    }

    //http://localhost:9090/roles/95852
    @PostMapping("/{id:[0-9]+}")
    @ApiOperation("查询角色权限列表")
    @PreAuthorize("hasAuthority('/erp_pwdb/role/read')")//检查用户是否有权限
    @ApiOperationSupport(order = 420)
    public JsonResult<List<PermissionListVO>> listPermission(
            @PathVariable @Range(min = 2, message = "请提交有效的ID值!") Long id) {
        log.debug("开始处理[查询角色详情]的请求,参数为：{}", id);
        List<PermissionListVO> permissionListVOS = service.listPermission(id);
        return JsonResult.ok(permissionListVOS);
    }

    //http://localhost:9090/roles/name
    @ApiOperation("根据角色名称查询角色详情")
    @ApiOperationSupport(order = 400)
    @GetMapping("/{name}")
    @PreAuthorize("hasAuthority('/erp_pwdb/role/read')")//检查用户是否有权限
    public JsonResult<RoleStandardVO> getStandardByName(@PathVariable("name") String name) {
        log.debug("开始处理查询角色权限详情的请求,参数为name={}", name);
        RoleStandardVO standardVO = service.getStandardByName(name);
        log.debug("查询到的结果为,{}", standardVO);
        return JsonResult.ok(standardVO);
    }
}
